﻿using Newtonsoft.Json;
using QRHelper;
using RFIDLIB;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using wareHouse;

namespace ShuoXiangProductionPlatform
{
    public partial class ProduceSystem : Form
    {
        public ProduceSystem()
        {
            this.FormBorderStyle = FormBorderStyle.None;
            this.IsMdiContainer = true;
            InitializeComponent();
            //初始化RFID，连接读卡器
            TeaRfidHelper.IninTeaRead();
            bool result = TeaRfidHelper.OpenTeaReader(url1);
            if (result)
            {
                this.label1.Text = "设备状态：连接成功";
            }
            else
            {
                this.label1.Text = "设备状态：连接失败";
            }
            TeaRfidHelper.StartTeaRead();
            TeaRfidHelper.OngetTagDatas += new TeaRfidHelper.GetTagDatas(TeaRfidHelper_OngetTagDatas);
            QR.InitQR(10000);
            if (QR.ConnectQrcode(6, 115200))
            {
                baoZhuangGuanLian.label9.Text = "二维码设备连接成功";
            }
            else
            {
                baoZhuangGuanLian.label9.Text = "二维码设备连接失败";
            }

            QR.OnGetQrcode += new QR.GetQrcode(QR_OnGetQrcode);
        }
        public delegate void ShowQR(string info);

        public void QR_OnGetQrcode(string info) 
        {
            if (InvokeRequired)
            {
                Invoke(new ShowQR(QR_OnGetQrcode), new object[] { info });
                return;
            }
            if (baoZhuangGuanLian.isscan == true && zhuangTi.isscan == false)
            {
                baoZhuangGuanLian.StartScan(info);
            }
            else if (baoZhuangGuanLian.isscan == false && zhuangTi.isscan == true)
            {
                zhuangTi.StartScan(info);
            }
        }


        public static ShengChanFuMa shengChanFuMa = new ShengChanFuMa();
        public static BaoZhuangGuanLian baoZhuangGuanLian = new BaoZhuangGuanLian();
        public static ZhuangTi zhuangTi = new ZhuangTi();
        //USB连接方式
        string url1 = "RDType=RL8000;CommType=USB;AddrMode=0;SerNum=100011";

        public delegate void ShowTag(string tagInfo);

        private void TeaRfidHelper_OngetTagDatas(string tagInfo)
        {
            if (InvokeRequired)
            {
                Invoke(new ShowTag(TeaRfidHelper_OngetTagDatas), new object[] { tagInfo });
                return;
            }
            if (shengChanFuMa.isstart == true && baoZhuangGuanLian.isstart == false)
            {
                shengChanFuMa.TeaRfidHelper_OngetTagDatas(tagInfo);
            }
            else if (shengChanFuMa.isstart == false && baoZhuangGuanLian.isstart == true)
            {
                baoZhuangGuanLian.StartCoding(tagInfo);
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定要退出系统吗？", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                this.Hide();
                Login.login.Show();
            }
            else
            {

            }
        }

        

        /// <summary>
        /// 显示生产赋码界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            shengChanFuMa.orderInformations.Clear();
            orderInfo orderInfo = new orderInfo { userId = Login.login.userInfo.logindata.userId, type = "1" };
            string param = JsonConvert.SerializeObject(orderInfo);
            HttpHelper httpHelper = new HttpHelper();
            string res = httpHelper.HTTPJsonPost("http://47.102.195.242:28083/orderList/p", param);

            var resjson = JsonConvert.DeserializeObject<JsonString.ApiMsgList<JsonString.orderlistdata>>(res);
            if (resjson.success.Equals("T"))
            {
                foreach (var item in resjson.data)
                {
                    foreach (var product in item.details)
                    {
                        OrderInformation orderInformation = new OrderInformation
                        {
                            订单号 = product.orderNo,
                            产品名称 = product.productName,
                            订单日期 = item.orderTime,
                            赋码数量 = product.productQuantity,
                            已赋码数量 = product.setedNum,
                            批次号 = product.batchId,
                            净含量 = 0,
                            产品ID = product.productId
                        };


                        shengChanFuMa.orderInformations.Add(orderInformation);
                        
                    }
                }
               shengChanFuMa.dataGridView1.DataSource = new List<OrderInformation> { shengChanFuMa.orderInformations[shengChanFuMa.pageSize - 2], shengChanFuMa.orderInformations[shengChanFuMa.pageSize - 1] };

            }

            shengChanFuMa.MdiParent = this;
            shengChanFuMa.Parent = this.panel1;
            shengChanFuMa.Dock = DockStyle.Fill;
            shengChanFuMa.Size = this.panel1.Size;
            shengChanFuMa.Show();
            baoZhuangGuanLian.Hide();
            zhuangTi.Hide();
            baoZhuangGuanLian.isstart = false;
            baoZhuangGuanLian.isscan = false;
            zhuangTi.isstart = false;
            zhuangTi.isscan = false;
            this.button1.BackColor = Color.FromArgb(216, 66, 84);
            this.button2.BackColor= Color.FromArgb(163, 0, 19);
            this.button3.BackColor = Color.FromArgb(163, 0, 19);
          
        }

        /// <summary>
        /// 显示包装关联界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
          
            baoZhuangGuanLian.MdiParent = this;
            baoZhuangGuanLian.Parent = this.panel1;
            baoZhuangGuanLian.Dock = DockStyle.Fill;
            baoZhuangGuanLian.Size = this.panel1.Size;
            baoZhuangGuanLian.Show();
            shengChanFuMa.Hide();
            zhuangTi.Hide();
            shengChanFuMa.isstart = false;
            shengChanFuMa.circleButton1.Text = "开始";
            baoZhuangGuanLian.isstart = true;
            baoZhuangGuanLian.isscan = true;
            zhuangTi.isscan = false;
            zhuangTi.isstart = false;
            this.button2.BackColor = Color.FromArgb(216, 66, 84);
            this.button1.BackColor = Color.FromArgb(163, 0, 19);
            this.button3.BackColor = Color.FromArgb(163, 0, 19);
        }

        /// <summary>
        /// 显示装提界面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            
            zhuangTi.MdiParent = this;
            zhuangTi.Parent = this.panel1;
            zhuangTi.Dock = DockStyle.Fill;
            zhuangTi.Size = this.panel1.Size;
            zhuangTi.Show();
            baoZhuangGuanLian.Hide();
            shengChanFuMa.Hide();
            shengChanFuMa.isstart = false;
            baoZhuangGuanLian.isstart = false;
            baoZhuangGuanLian.isscan = false;
            shengChanFuMa.circleButton1.Text = "开始";
            this.button3.BackColor = Color.FromArgb(216, 66, 84);
            this.button2.BackColor = Color.FromArgb(163, 0, 19);
            this.button1.BackColor = Color.FromArgb(163, 0, 19);
        }
    }
}
